﻿/* QF classbuilder for Enlib */
using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Logging;

#region SYS_Function实体类
public class SYS_Function
{
	private string m_FunctionID;
	private string m_Name;
	private string m_Description;
	private string m_Tag;
	
	public SYS_Function ()
	{
		m_FunctionID="";
		m_Name="";
		m_Description="";
		m_Tag="";
	}
	
	public string FunctionID
	{
		get{return m_FunctionID;}
		set{m_FunctionID=value;}
	}
	public string Name
	{
		get{return m_Name;}
		set{m_Name=value;}
	}
	public string Description
	{
		get{return m_Description;}
		set{m_Description=value;}
	}
	public string Tag
	{
		get{return m_Tag;}
		set{m_Tag=value;}
	}
}
#endregion

#region SYS_Function管理类
public partial class LogicLayer
{
	public bool AddSYS_Function(SYS_Function theSYS_Function)
	{
		try
		{
			Database db = DatabaseFactory.CreateDatabase("connStr");
			string sql = "insert into SYS_Function (FunctionID,Name,Description,Tag) values (@FunctionID,@Name,@Description,@Tag)";
			System.Data.Common.DbCommand cmd = db.GetSqlStringCommand(sql);
			
			db.AddInParameter(cmd, "@FunctionID", DbType.String, theSYS_Function.FunctionID);
			db.AddInParameter(cmd, "@Name", DbType.String, theSYS_Function.Name);
			db.AddInParameter(cmd, "@Description", DbType.String, theSYS_Function.Description);
			db.AddInParameter(cmd, "@Tag", DbType.String, theSYS_Function.Tag);
			
			db.ExecuteNonQuery(cmd);
			return true;
		}
		catch(Exception ex)
		{
			Logger.Write("AddSYS_Function:" + ex.Message );
			return false;
		}
	}
	
	public SYS_Function GetSYS_FunctionFromDataReader(IDataReader dataReader)
	{
		try
		{
			SYS_Function result = new SYS_Function();
			
			if (dataReader["FunctionID"] != null && dataReader["FunctionID"] != System.DBNull.Value )
				result.FunctionID = (string)dataReader["FunctionID"];
			if (dataReader["Name"] != null && dataReader["Name"] != System.DBNull.Value )
				result.Name = (string)dataReader["Name"];
			if (dataReader["Description"] != null && dataReader["Description"] != System.DBNull.Value )
				result.Description = (string)dataReader["Description"];
			if (dataReader["Tag"] != null && dataReader["Tag"] != System.DBNull.Value )
				result.Tag = (string)dataReader["Tag"];
			
			return result;
		}
		catch(Exception ex)
		{
			Logger.Write("GetReaderSYS_Function:" + ex.Message);
			return null;
		}
	}
	
	public SYS_Function GetSYS_Function(string FunctionID)
	{
		try
		{
			Database db = DatabaseFactory.CreateDatabase("connStr");
			string sql = "select * From SYS_Function where FunctionID=@FunctionID";
			
			System.Data.Common.DbCommand cmd = db.GetSqlStringCommand(sql);
			db.AddInParameter(cmd, "@FunctionID", DbType.String, FunctionID);
			
			SYS_Function result = null;
			using (IDataReader dataReader = db.ExecuteReader(cmd))
			{
				if (dataReader.Read())
				{
					result = GetSYS_FunctionFromDataReader( dataReader );
				}
			}
			return result;
		}
		catch(Exception ex)
		{
			Logger.Write("GetSYS_Function:" + ex.Message);
			return null;
		}
	}
	
	public bool DelSYS_Function(string FunctionID)
	{
		try
		{
			Database db = DatabaseFactory.CreateDatabase("connStr");
			string sql = "delete From SYS_Function where FunctionID=@FunctionID";
			
			System.Data.Common.DbCommand cmd = db.GetSqlStringCommand(sql);
			db.AddInParameter(cmd, "@FunctionID", DbType.String, FunctionID);
			
			db.ExecuteNonQuery(cmd);
			return true;
		}
		catch(Exception ex)
		{
			Logger.Write("DelSYS_Function:" + ex.Message);
			return false;
		}
	}
	
	public bool UpdateSYS_Function(SYS_Function theSYS_Function)
	{
		try
		{
			Database db = DatabaseFactory.CreateDatabase("connStr");
			string sql = "update SYS_Function set Name=@Name,Description=@Description,Tag=@Tag where FunctionID=@FunctionID";
			System.Data.Common.DbCommand cmd = db.GetSqlStringCommand(sql);
			
			db.AddInParameter(cmd, "@Name", DbType.String, theSYS_Function.Name);
			db.AddInParameter(cmd, "@Description", DbType.String, theSYS_Function.Description);
			db.AddInParameter(cmd, "@Tag", DbType.String, theSYS_Function.Tag);
			db.AddInParameter(cmd, "@FunctionID", DbType.String, theSYS_Function.FunctionID);
			
			db.ExecuteNonQuery(cmd);
			return true;
		}
		catch(Exception ex)
		{
			Logger.Write("UpdateSYS_Function:" + ex.Message);
			return false;
		}
	}
	
	public List<SYS_Function> ListSYS_Function()
	{
		try
		{
			Database db = DatabaseFactory.CreateDatabase("connStr");
			string sql = "select * from SYS_Function";
			System.Data.Common.DbCommand cmd = db.GetSqlStringCommand(sql);
			
			 List<SYS_Function> result = new  List<SYS_Function>();
			using (IDataReader dataReader = db.ExecuteReader(cmd))
			{
				while (dataReader.Read())
				{
					SYS_Function obj = GetSYS_FunctionFromDataReader( dataReader );
					if(obj != null)
						result.Add(obj);
				}
			}
			return result;
		}
		catch(Exception ex)
		{
			Logger.Write("ListSYS_Function:" + ex.Message);
			return null;
		}
	}
	
}
#endregion